import { createSvg } from "./svg.js"
// import { pointsFnMap } from "./renderShape.js"
import { _d } from "./path.js"
// import { polarPoints } from '../math/points/polarPoints.js'
import { gridCrossPoints, gridCellPoints, polarPoints, polarGridPoints, spiralPoints, } from '../math/points/index.js'
import { _k } from "../math/index.js"

const pointsFnMap = {
    polygon: polarPoints,
    grid: gridCrossPoints,
    gridCell: gridCellPoints,
    polarGrid: polarGridPoints,
    spiral: spiralPoints
}
export const _motion = (options = {}, props) => {
    let { id, dur = '6s', begin = "0s", path = {}, type = "dashoffset" } = options

    if (type === 'dashoffset') {
        // dashoffset动画
        let to = _k(Math.PI * 100)

        return createSvg("animate", {
            attributeType: 'XML',
            attributeName: 'stroke-dashoffset',
            repeatCount: 'indefinite',
            from: '0',
            to,
            dur,

        })
    }
    let mpath
    if (id) {
        mpath = createSvg("mpath", {
            'xlink:href': '#' + id
        })
    }

    let { shape = 'polygon', o = [400, 300], r = 100, n = 3, broken = false, closed = true } = path
    // debugger
    let points = pointsFnMap[shape]({
        o, r, n,
        ...path,
        o: [0, 0]
    })
    let d = _d(points, {
        broken,
        closed
    })

    // 路径动画
    return createSvg("animateMotion", {
        dur,
        begin,
        repeatCount: 'indefinite',
        rotate: 'auto',
        fill: 'remove',
        path: d
    }, {}, mpath)


}